package com.lx.chong.service.impl;

import com.lx.chong.dto.UserDto;
import com.lx.chong.model.User;
import com.lx.chong.repository.UserRepository;
import com.lx.chong.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.jpa.criteria.compile.ExplicitParameterInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.*;
import java.util.List;

/**
 * @author chong
 * @create 2019/5/14
 * Desc:
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private EntityManager entityManager;

    @Override
    public User save(User user) {
        return userRepository.save(user);
    }

    @Override
    public User findOne(Long id) {
        return userRepository.findOne(id);
    }

    @Override
    public List<User> getUserListPage(Integer startNum, Integer total) {
        return userRepository.getUserListPage(startNum * total, total);
    }

    @Override
    public Long getTotalPage() {
        return userRepository.getTotalPage();
    }

    @Override
    public User queryUserByAccountOrMobile(String account) {
        return userRepository.findAllByAccountAndIfDeleteAndUseMark(account, 0, 1);
    }

    @Override
    public List<User> queryUserList(UserDto userDto) {
        String sql = "select * from user where ifDelete = 0";
        if (StringUtils.isNotBlank(userDto.getName())) {
            sql = sql + " and perName like ?1";
        }
        if (StringUtils.isNotBlank(userDto.getMobile())) {
            sql = sql + " and Mobile =?2 ";
        }
        if (StringUtils.isNotBlank(userDto.getSort())) {
            sql = sql + " order by " + userDto.getSort();
        }
        sql = sql + " limit "+userDto.getPage()*userDto.getSize()+","+userDto.getSize();
        Query nativeQuery = entityManager.createNativeQuery(sql, User.class);

        if (StringUtils.isNotBlank(userDto.getName())) {
            nativeQuery.setParameter(1, "%" + userDto.getName() + "%");
        }
        if (StringUtils.isNotBlank(userDto.getMobile())) {
            nativeQuery.setParameter(2, userDto.getMobile());
        }
        return nativeQuery.getResultList();
    }

    @Override
    public Long getTotalPage(UserDto userDto) {
        String sql = "select * from user where ifDelete = 0";
        if (StringUtils.isNotBlank(userDto.getName())) {
            sql = sql + " and perName like ?1";
        }
        if (StringUtils.isNotBlank(userDto.getMobile())) {
            sql = sql + " and Mobile = ?2";
        }
        Query nativeQuery = entityManager.createNativeQuery(sql);
        if (StringUtils.isNotBlank(userDto.getName())) {
            nativeQuery.setParameter(1, "%" + userDto.getName() + "%");
        }
        if (StringUtils.isNotBlank(userDto.getMobile())) {
            nativeQuery.setParameter(2, userDto.getMobile());
        }
        Integer size = nativeQuery.getResultList().size();
        return size.longValue();
    }

    @Override
    public List<User> findAllByIfDelete() {
        return userRepository.findAllByIfDelete(0);
    }
}
